import { Defect, FilterRule, TypeRule, LogicalRule } from "./types";
import { preprocess } from "./utils";

// 测试用例
const testData: Defect[] = [
  {
    x: 100,
    y: 200,
    type: "scratch",
    area: 15,
    longSide: 4,
    shortSide: 3,
    score: 0.9,
  },
  {
    x: 100,
    y: 200,
    type: "dent",
    area: 8,
    longSide: 6,
    shortSide: 2,
    score: 0.7,
  },
  {
    x: 100,
    y: 200,
    type: "scratch",
    area: 20,
    longSide: 3,
    shortSide: 5,
    score: 0.8,
  },
  {
    x: 150,
    y: 250,
    type: "crack",
    area: 12,
    longSide: 7,
    shortSide: 4,
    score: 0.85,
  },
  {
    x: 150,
    y: 250,
    type: "dent",
    area: 10,
    longSide: 5,
    shortSide: 3,
    score: 0.6,
  },
  {
    x: 50,
    y: 300,
    type: "scratch",
    area: 18,
    longSide: 4,
    shortSide: 6,
    score: 0.95,
  },
];

// 运行测试
const processedData = preprocess(testData, true);
console.log("原始数据：");
processedData.print();

// 测试过滤
const rules: LogicalRule = {
  op: "AND",
  rules: [
    { field: "flaws.dent.stats.totalArea", op: "<=", value: 10 }, // dent 类型总面积 <= 10
    { type: "scratch", op: "not_exists" }, // 不包含 scratch 类型
    {
      op: "OR",
      rules: [
        { field: "flaws.crack.stats.count", op: ">=", value: 1 }, // crack 类型缺陷数 >= 1
        { type: "dent", op: "exists" }, // 存在 dent 类型
      ],
    },
  ],
};
console.log("\n过滤后数据：");
const filteredData = processedData.filterByTypeStats(rules);
filteredData.print();
